const connection = require("../db/connection");
const input = require("scanline");
const checkProductExist = require("./checkProductExist");
const menu = require("../UI/menu");
const checkBox = require("../UI/checkBox");

async function changePart(){
    showPath();
    const name = await input("请输入你要修改的商品信息对应的名称:");
    const oldProduct = await checkProductExist(name);

    let sp = {
        "名称":"",
        "价格":0,
        "种类":"",
        "数量":0,
    };
    const zl = [
        "食物",
        "服装",
        "工具",
        "生活用品",
        "电子产品",
    ];

    
    const checkboxSelect = await checkBox(
        Object.keys(sp),
        "请选择你要修改的字段(按下空格选择,按a全选,按回车确定你的选择):\n",
    );
    sp = oldProduct;

    for(let select of checkboxSelect.selects){
        if(select === "种类"){
            const menuSelect = await menu(zl,"请选择你的商品种类：\n");
            sp.种类 = menuSelect.text;
            continue;
        }
        sp[select] = await input(`请输入该商品的新${select}:`);
    };

    connection.query("update product set 名称=?,价格=?,种类=?,数量=? where 名称=?",[
        sp["名称"],
        parseFloat(sp["价格"]),
        sp["种类"],
        parseInt(sp["数量"]),
        name,
    ],
    (error,_)=>{
        if(error){
            console.log("修改信息失败！");
            process.exit(1);
        }
        console.log("\033[33m已成功修改该商品部分信息!\033[0m");
    });
    connection.end();

}


module.exports = changePart;